<?php
namespace app\m\controller;

use app\common\controller\FrontController;
use app\common\service\GoodsService;
use app\common\service\MemberService;

class Goods extends FrontController
{
    public function index(){
        if(request()->isAjax()){
            $param = input('param.');
            $goodsService = new GoodsService();
            $jieguo = $goodsService->getGoodsPager($param);
            if($jieguo['err_code'] != 0){
                $this->error('商品获取失败');
            }
            else{
                $this->success('商品获取成功', '',$jieguo['data']);
            }
        }

        return $this->fetch();
    }

    public function detail()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $goodsService = new GoodsService();
        $jieguo = $goodsService->getGoods($id);
        if($jieguo['err_code'] != 0){
            $this->error('商品获取失败');
        }
        $goodsModel = $jieguo['data'];
//        dump($goodsModel);exit;

        // 请选择 规格的标题
        $spec_titles = '';
        if (!empty($goodsModel['has_option'])) {
            $tmp = [];
            foreach ($goodsModel['specs'] as $spec) {
                $tmp[] = $spec['title'];
            }
            $spec_titles = join('、', $tmp);
            unset($tmp);
        }
        $options = $goodsModel['options'];
        $fanhui = [];
        foreach ($options as $option) {
            $fanhui[] = [
                'id' => $option['id'],
                'title' => $option['title'],
                'thumb' => $option['thumb'],
                'price' => $option['market_price'],
                'stock' => $option['stock'],
                'specs' => '_' . $option['specs'] . '_',
            ];
        }
        $this->assign('options',$fanhui);

        $this->assign('goodsModel',$goodsModel);
        $this->assign('spec_titles',$spec_titles);
        return $this->fetch();
    }
    public function get_detail_content()
    {
        if(!request()->isAjax()){
            return '提交方法不正确';
        }
        $id = input('id');
        if (empty($id)) {
            return '参数错误';
        }
        $goodsService = new GoodsService();
        $jieguo = $goodsService->getGoods($id);
        if($jieguo['err_code'] != 0){
            return '商品获取失败';
        }
        $goodsModel = $jieguo['data'];

        return imgLazyJs($goodsModel['goods_content']);
    }

    public function add_goods_to_cart()
    {
        if(!request()->isAjax()){
            $this->error('提交方法不正确');
        }
        $goodsId = input('id');
        $optionId = input('option_id');
        $total = input('total',1);
        $memberService = new MemberService();
        $jieguo = $memberService->addGoodsToCart($this->getMemberId(), $goodsId, $total,1,$optionId);
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $this->success('加入购物车成功','',$jieguo['data']);
    }

    /**
     * 减购物车
     */
    public function minus_goods_to_cart()
    {
        if(!request()->isAjax()){
            $this->error('提交方法不正确');
        }
        $cartId = input('id');
        $total = input('total',1);
        $memberService = new MemberService();
        $jieguo = $memberService->minusGoodsToCart($this->getMemberId(), $cartId, $total);
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $this->success('更新购物车成功','',$jieguo['data']);
    }

    public function del_goods_to_cart()
    {
        if(!request()->isAjax()){
            $this->error('提交方法不正确');
        }
        $ids = input('cart_ids');
        $memberService = new MemberService();
        $jieguo = $memberService->delCartByIds($this->getMemberId(),explode(',', $ids));
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('删除成功');
        }
    }
}
